unit Unit12;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.AppEvnts, superobject;

type
  TForm12 = class(TForm)
    mmo1: TMemo;
    btn1: TButton;
    aplctnvnts1: TApplicationEvents;
    procedure btn1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure aplctnvnts1Message(var Msg: tagMSG; var Handled: Boolean);
  private
    { Private declarations }
    procedure OnPowerMsg(var Msg: TMessage); message WM_POWERBROADCAST;
  public
  end;

  TSyWebViewKernel = (wvkChrome, wvkIE);

  TSyCookieRec = record
  public
    name, value, domain, path: string;
    secure, httponly, hasExpries: Boolean;
    creation, lastAccess, expires: TDateTime;

    class function CreateFromJson(AJson: ISuperObject): TSyCookieRec; static;
  end;

  TSyOpenTabAndSetCookie = class
    url,
    token: string;
    cookies: array of TSyCookieRec;
    webKernal: TSyWebViewKernel;
  public
    constructor FromJson(AJson: string);
  end;

var
  Form12: TForm12;

implementation
uses
  System.DateUtils;

{$R *.dfm}

procedure TForm12.aplctnvnts1Message(var Msg: tagMSG; var Handled: Boolean);
begin
  if Msg.message = WM_POWERBROADCAST then
  begin
    mmo1.Clear;
    mmo1.Lines.Add(format('[%s] [aplctnvnts1Message] msg wParam: %d, lParam: %d', [DateTimeToStr(Now), Msg.WParam, Msg.LParam]));
    mmo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + '11.txt');
    Handled := true;
  end;

end;

procedure TForm12.btn1Click(Sender: TObject);
const
  str = '{"token":"xht","url":"www.baidu.com","webKernal":1,"cookies":{"name":"xht_BAIDUID","value":"123456",'
    + '"domain":".baidu.com","path":"/","secure":false,"httponly":false,"hasExpires":false,"creation":"2020-01-10 09:00:00.000","lastAccess":"2020-01-10 09:00:00.000","expires":"2020-12-10 09:00:00.000"}}';
var
  tab: TSyOpenTabAndSetCookie;
begin
  tab := TSyOpenTabAndSetCookie.FromJson(mmo1.Text);
  try
    mmo1.Lines.Add(tab.url)
  finally
    FreeAndNil(tab);
  end;
end;

procedure SaveToFile(Data: String);
var
  strLst: TStrings;
begin
  strLst := TStringList.Create;
  try
    strLst.Add(data);
    strLst.SaveToFile('c:\111.txt');
  finally
    strLst.Free;
  end;
end;

procedure TForm12.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//  CanClose := False;
end;

procedure TForm12.OnPowerMsg(var Msg: TMessage);
begin

  mmo1.Lines.Add(format('[%s] [OnPowerMsg] msg wParam: %d, lParam: %d', [DateTimeToStr(Now), Msg.WParam, Msg.LParam]));
  mmo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + '11.txt');

    Msg.Result := 0;
end;

{ TSyCookieRec }

class function TSyCookieRec.CreateFromJson(AJson: ISuperObject): TSyCookieRec;

  function StrToDateTimeEx(ADateTimeStr: string): TDateTime;
  var
    frm: TFormatSettings;
  begin
    frm := TFormatSettings.Create(LOCALE_USER_DEFAULT);
    frm.LongDateFormat := 'yyyy-mm-dd hh:nn:ss';
    frm.DateSeparator := '-';
    Result := StrToDateTime(ADateTimeStr, frm);
  end;

begin
  Result.name := AJson.S['name'];
  Result.value := AJson.S['value'];
  Result.domain := AJson.S['domain'];
  Result.path := AJson.S['path'];
  Result.secure := AJson.B['secure'];
  Result.httponly := AJson.B['httponly'];
  Result.hasExpries := AJson.B['hasExpries'];
  if AJson.S['creation'].IsEmpty then
    Result.creation := Now
  else
    Result.creation := StrToDateTimeEx(AJson.S['creation']);
  if AJson.S['lastAccess'].IsEmpty then
    Result.lastAccess := 0
  else
    Result.lastAccess := StrToDateTimeEx(AJson.S['lastAccess']);
  if Result.hasExpries then
    Result.expires := StrToDateTimeEx(AJson.S['expires'])
  else
    Result.expires := 0;
end;

{ TSyOpenTabAndSetCookie }

constructor TSyOpenTabAndSetCookie.FromJson(AJson: string);
var
  I: Integer;
  obj: ISuperObject;
  cookies: TSuperArray;
begin
  obj := SO(AJson);
  Self.url := obj.S['url'];
  Self.token := obj.S['token'];
  Self.webKernal := TSyWebViewKernel(obj.I['webKernal']);

  if obj.O['cookies'] = nil then Exit;

  if obj.O['cookies'].IsType(stObject) then
  begin
    SetLength(Self.cookies, 1);
    Self.cookies[0] := TSyCookieRec.CreateFromJson(obj.O['cookies']);
  end
  else if obj.O['cookies'].IsType(stArray) then
  begin
    cookies := obj.O['cookies'].AsArray;
    SetLength(Self.cookies, cookies.Length);
    for I := 0 to cookies.Length - 1 do
    begin
      Self.cookies[I] := TSyCookieRec.CreateFromJson(cookies.O[I]);
    end;
  end;

end;

end.
